//
//  HtmlViewController.m
//  Inforcollection
//
//  Created by macbook on 15/12/8.
//  Copyright © 2015年 oceansoft.liw. All rights reserved.
//

#import "HtmlViewController.h"

#define KProgressHeight 2.0f

@interface HtmlViewController ()

@end

@implementation HtmlViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self setHtmlNavLeftButton];
    
    [self addWebView];
    
    [self addProgressView];
    
    [self addTimer];
}

-(void)setHtmlNavLeftButton
{
    UIButton *backBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 40 , 40)];
    backBtn.backgroundColor = [UIColor clearColor];
    
    [backBtn setImage:[UIImage imageNamed:@"bar_left_image"] forState:UIControlStateNormal];
    
    [backBtn addTarget:self action:@selector(backToWhere:) forControlEvents:UIControlEventTouchUpInside];
    
    [self setNaviBarLeftBtn:backBtn];
}
-(void)addWebView
{
    _currentWebView=[[UIWebView alloc]initWithFrame:Rect(0, StatuBarHeight+NaviBarHeight, ScreenWidth, ScreenHeight-StatuBarHeight-NaviBarHeight)];
    
    _currentWebView.delegate=self;
    
    _currentWebView.scrollView.showsHorizontalScrollIndicator=NO;
    
    _currentWebView.scrollView.showsVerticalScrollIndicator=NO;
    
    _currentWebView.scrollView.bounces=NO;
    
    _currentWebView.contentMode=UIViewContentModeScaleAspectFit;
    
    [self.view addSubview:_currentWebView];
}

-(void)addProgressView
{
    _progressView = [[UIProgressView alloc]initWithFrame:CGRectMake(0,NaviBarHeight+StatuBarHeight-KProgressHeight, ScreenWidth, KProgressHeight)];
    
    _progressView.progress=0.0f;
    
    _progressView.progressTintColor=[UIColor clearColor];
    
    _progressView.backgroundColor=[UIColor clearColor];
    
    _progressView.progressImage=[UIImage imageNamed:@"progressImage"];
    
    [self addProgressWithView:_progressView];
}
-(void)addTimer
{
    _timer = [NSTimer timerWithTimeInterval:0.1 target:self selector:@selector(changeProgress) userInfo:nil repeats:YES];
    
    [[NSRunLoop currentRunLoop]addTimer:_timer forMode:NSRunLoopCommonModes];
}

-(void)startProgress
{
    _progressView.hidden=NO;
    _progressView.progress=0.0f;
    [_timer setFireDate:[NSDate distantPast]];
}

-(void)stopProgress
{
    _progressView.hidden=YES;
    _progressView.progress=1.0f;
    [_timer setFireDate:[NSDate distantFuture]];
}

-(void)changeProgress
{
    _progressView.progress+=0.02f;
}

-(void)backToWhere:(id)sender
{
    if ([_currentWebView canGoBack]) {
        [_currentWebView goBack];
    }
    else{
        [self.navigationController popViewControllerAnimated:YES];
    }
}
#pragma mark------WebView Delegate
-(void)webViewDidStartLoad:(UIWebView *)webView
{
    [self startProgress];
}
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
    [self stopProgress];
}

-(void)loadWebView
{
    
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}
@end
